<?php

namespace App\Admin\Controllers;

use Encore\Admin\Facades\Admin;
use App\Models\Department;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Show;
use Encore\Admin\Tree;
use Encore\Admin\Widgets\Box;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class DepartmentController extends AdminController
{
    use ModelForm;
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'App\Models\Department';

    protected $header = '部门管理';


    public function index(Content $content)
    {
        return Admin::content(function (Content $content) {
            $content->header($this->header);
            $content->description('部门列表');

            $content->row(function (Row $row) {
                $row->column(6, $this->treeView()->render());

                $row->column(6, function (Column $column) {
                    $form = new \Encore\Admin\Widgets\Form();
                    $form->action(admin_base_path('auth/departments'));

                    $form->text('name', '部门名称');
                    $form->textarea('description', '部门描述信息');
                    $form->number('order', '排序序号')->default(0);
                    $form->select('parent_id','父类名称')->options(Department::selectOptions());
                    $form->select('charger_id', '部门负责人')->options(User::all()->pluck('name', 'id'));
                    $form->text('department_code', '部门编码');
                    $form->select('type', '机构类型')->options([
                        '网点' => '网点',
                        '机关部室' => '机关部室',
                        '一级支行' => '一级支行',
                        '二级分行' => '二级分行',
                        '一级分行' => '一级分行'
                    ]);
                    $form->hidden('_token')->default(csrf_token());

                    $column->append((new Box(trans('admin.new'), $form))->style('success'));
                });
            });
        });
    }

    protected function treeView()
    {
        return Department::tree(function (Tree $tree) {
            return $tree;
        });
    }

    public function create(Content $content)
    {
        return Admin::content(function (Content $content) {
            $content->header($this->header);
            $content->description('添加部门');
            $content->body($this->form());
        });
    }

    public function edit($id, Content $content)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->header);
            $content->description('编辑部门');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Department);



        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Department::findOrFail($id));



        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Department);

        $form->display('id', 'ID');

        $form->text('name', '部门名称');
        $form->textarea('description', '部门描述信息');
        $form->number('order', '排序序号');
        $form->select('parent_id', '父类名称')->options(Department::selectOptions());
        $form->select('charger_id', '部门负责人')->options(User::all()->pluck('name', 'id'));
        $form->text('department_code', '部门编码');
        $form->select('type', '机构类型')->options([
            '网点' => '网点',
            '机关部室' => '机关部室',
            '一级支行' => '一级支行',
            '二级分行' => '二级分行',
            '一级分行' => '一级分行'
        ]);

        return $form;
    }

    public function getDepartmentOptions()
    {
        return DB::table('departments')->select('id','name as text')->get();
    }

    public function getDepartment(Request $request)
    {
        //return DB::table('departments')->where('parent_id','=',2)->get(['id', DB::raw('name as text')]);
        return DB::table('departments')->get(['id', DB::raw('name as text')]);
    }
}
